﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Orderers;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Warehousing;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Warehousings
{
    //入库表添加
    public class StorageCreateHandler : IRequestHandler<StorageCreateCommend, Result>
    {
        private readonly IBaseRepository<Storage> Storage;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public StorageCreateHandler(IMapper mapper, IBaseRepository<Storage> Storage)
        {
            this.mapper = mapper;
            this.Storage = Storage;
        }

        public async Task<Result> Handle(StorageCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<Storage>(request);
            string sql = "insert into Storage values(@StorageId,@StorageNo,@StorageType,@WarehouseId,@StorageDate,@StorageRemark,@PurchasingTypeId,@PurchasinId,@QualityState,@StorageState,@MakeSingle," +
                "@MakeDepartment,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = Storage.Add(sql, new
            {
                StorageId = AddData.StorageId,
                StorageNo = AddData.StorageNo,
                StorageType = AddData.StorageType,
                WarehouseId = AddData.WarehouseId,
                StorageDate = AddData.StorageDate,
                StorageRemark = AddData.StorageRemark,
                PurchasingTypeId = AddData.PurchasingTypeId,
                PurchasinId = AddData.PurchasinId,
                QualityState = AddData.QualityState,
                StorageState = AddData.StorageState,
                MakeSingle = AddData.MakeSingle,
                MakeDepartment = AddData.MakeDepartment ,
                IsDelete = AddData.IsDelete,
                Handle = AddData.Handle = "李四",
                HandleContext = AddData.HandleContext = "添加",
                HandleTime = AddData.HandleTime = DateTime.Now,
                Pid = AddData.Pid = 0,
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
